<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>商家查看退款申请</title>
    <link href="css/bootstrap.css" rel="stylesheet">
    <script src="js/jquery-3.6.4.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/popper.js/2.9.3/umd/popper.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <style>
        #myPopover {
            display: none;
            position: absolute;
            top: 30px;
            left: 50px;
            background-color: #fff;
            border: 1px solid #ccc;
            padding: 10px;
        }
    </style>
</head>
<body>
<div id="top"></div>
<div class="panel panel-default" style="margin: 5% auto;width: 95%;">
    <div class="panel-heading">
        <h3 class="panel-title"><span class="glyphicon glyphicon-equalizer"></span>&nbsp;&nbsp;退款申请</h3>
    </div>
    <div class="panel-body">
        <table class="table table-striped table-bordered table-hover">
            <tr>
                <td colspan="3">
                    <table class="table table-striped table-bordered table-hover">
                        <tr class="info">
                            <th>序号</th>
                            <th>订单编号</th>
                            <th>退款商品</th>
                            <th>退款金额</th>
                            <th>退款原因</th>
                            <th>退款人</th>
                            <th>操作</th>
                        </tr>
                        <tbody id="refund-part"></tbody>
                    </table>
                </td>
            </tr>
        </table>
    </div>
</div>

<script type="text/javascript">
    $(function () {
        $("#top").load("top2.html")
        //请求数据渲染页面
        $.ajax({
            url: "/PDD/order?method=showRefundForSeller",
            dataType: "json",
            type: "POST",
            success: function (data) {
                let refunds = data.data
                for (let i = 1; i <= refunds.length; i++) {
                    let refund = refunds[i - 1]
                    let html2 = `<tr>
  <th>${i}</th>
  <th>${refund.oId}</th>
  <th>${refund.pName}</th>
  <th>${refund.iCount.toFixed(2)}</th>
  <th>${refund.reason}</th>
  <th>${refund.username}</th>
  <th>
  <button id="refund${i}" type="button" class="btn btn-danger"></button>
  <div id="myPopover${i}">
  <button id="agree${i}" type="button" class="btn btn-danger">同意</button>
  <button id="reject${i}" type="button" class="btn btn-danger">拒绝</button>
  </div>
  </th>
</tr>`
                    $("#refund-part").append(html2)
                    let myPopover=$("#myPopover"+i);
                    let refundBtn=$("#refund"+i);
                    let agree=$("#agree"+i);
                    let reject=$("#reject"+i);
                    if(refund.rState===1){
                        refundBtn.html("审核")
                        myPopover.hide();  // 隐藏弹出框
                        refundBtn.click(function() {
                            let position = $("#refund"+i).offset();  // 获取按钮位置
                            let posX = position.left; // 计算弹出框位置的 X 坐标
                            let posY = position.top+25;  // 计算弹出框位置的 Y 坐标
                            myPopover.css({top: posY, left: posX}).show(); // 设定弹出框位置并显示它
                        });
                        $(document).mouseup(function(e) {  // 点击页面其它部分时，隐藏弹出框
                            let container = myPopover;
                            if (!container.is(e.target) && container.has(e.target).length === 0) {
                                container.hide();
                            }
                        });
                        //同意点击事件
                        agree.click(function (){
                            //修改双方金额
                            $.ajax({
                                url:"/PDD/user?method=modifyBalanceOfRefund",
                                data:{
                                    "buyerId":refund.buyerId,
                                    "count":refund.iCount,
                                },
                                dataType:"json",
                                success:function (data){
                                    if(data!=null&&data.code!==-1){
                                        //修改状态
                                        $.ajax({
                                            url:"/PDD/order?method=modifyRefundState",
                                            data:{
                                                "iid":refund.iId,
                                                "state":2
                                            },
                                            dataType: "json",
                                            success:function (data){
                                                if(data.code===1){
                                                    alert("退款成功")
                                                    location.reload()
                                                }
                                            }
                                        })
                                    }else{
                                        alert("发生错误")
                                    }
                                }
                            })
                        })
                        //拒绝点击事件
                        reject.click(function (){
                            $.ajax({
                                url:"/PDD/order?method=modifyRefundState",
                                data:{
                                    "iid":refund.iId,
                                    "state":1
                                },
                                dataType: "json",
                                success:function (){
                                    location.reload()
                                }
                            })
                        })
                    }else if(refund.rState===0){
                        myPopover.hide();  // 隐藏弹出框
                        refundBtn.html("已拒绝")
                    }else if(refund.rState===2){
                        myPopover.hide();  // 隐藏弹出框
                        refundBtn.html("已通过")
                    }



                    /*//修改按钮绑定点击事件
                    $("#refund"+i).click(function (){
                        //跳转到修改页面 把pid传过去
                        //window.location.href="/PDD/modifyProduct.html?pId="+product.pId
                        $("#popover").toggle()
                    })*/
                }
            }
        })

    })
</script>
</body>
</html>